﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.Objects.DataClasses;
using System.Runtime.Serialization;
using System.Reflection;

using Httlgd.DataCenter.Services.Core.Common;

namespace Httlgd.DataCenter.Services.Core
{
    public abstract class EntityBase : EntityObject
    {
        public const string CreateDatePropertyName = "CreateDate";
        public const string LastUpdateDatePropertyName = "LastUpdateDate";

        /// <summary>
        /// Initializes a new instance of the EntityBase class.
        /// </summary>
        public EntityBase()
        {
        }

        /// <summary>
        /// Gets entity key value 
        /// </summary>
        public object Key
        {
            get
            {
                if (EntityKey == null ||
                    EntityKey.EntityKeyValues == null ||
                    EntityKey.EntityKeyValues.Count() == 0)
                {
                    return null;
                }

                return EntityKey.EntityKeyValues[0].Value;
            }
        }

        /// <summary>
        /// Gets entity CreateDate 
        /// </summary>
        public DateTime GetCreateDate
        {
            get
            {
                object o = this.GetProperty(CreateDatePropertyName);
                if (o == null || !(o is DateTime))
                {
                    return DateTime.MinValue;
                }
                else
                {
                    return (DateTime)o;
                }
            }
        }

        /// <summary>
        /// Gets entity LastUpdateDate
        /// </summary>
        public DateTime GetLastUpdateDate
        {
            get
            {
                object o = this.GetProperty(LastUpdateDatePropertyName);
                if (o == null || !(o is DateTime))
                {
                    return DateTime.MinValue;
                }
                else
                {
                    return (DateTime)o;
                }
            }
        }

        /// <summary>
        /// Sets CreateDate property
        /// </summary>
        public void SetCreateDate()
        {
            this.SetProperty(CreateDatePropertyName, DateTime.Now);
        }

        /// <summary>
        /// Sets LastUpdateDate property
        /// </summary>
        public void SetLastUpdateDate()
        {
            this.SetProperty(LastUpdateDatePropertyName, DateTime.Now);
        }
    }
}
